package com.ioffice.export.excel.engine.data.excel.impl.line;

import java.util.ArrayList;
import java.util.List;

import com.ioffice.exception.IOfficeException;
import com.ioffice.export.excel.data.ExcelSheetDataInfo;
import com.ioffice.export.excel.engine.data.excel.impl.AbstractEngineDataSheetCompent;
import com.ioffice.export.excel.sheet.cell.SheetCell;
import com.ioffice.export.excel.sheet.line.SheetParamsLine;
import com.ioffice.export.excel.type.ExcelSheetLineType;
import com.ioffice.export.excel.type.ExcelSheetType;
import static com.ioffice.export.excel.engine.data.excel.EngineDataSheetCell.getSheetCell;
import static com.ioffice.export.excel.engine.data.excel.EngineDataSheetCell.addChidrenSheetCell;

public class EngineDataExcelSheetLineParams extends AbstractEngineDataSheetCompent{

	private ExcelSheetDataInfo parentData=null;
	private ExcelSheetDataInfo childData=null;
	private SheetCell cell=null;
	private int deepCount=0;
	private int leafCount=0;
	
	private int level=0;
	
	public EngineDataExcelSheetLineParams() {
		compent=new SheetParamsLine();
	}
	/**
	 * 
	 * @param nodeLevel 开始节点的级别
	 */
	public  EngineDataExcelSheetLineParams(int nodeLevel){
		this();
		this.level=nodeLevel;
	}
	
	@Override
	public void data2SheetCompent(List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType) throws IOfficeException {
		ExcelSheetLineType sheetLineType=(ExcelSheetLineType)sheetType;
		leafCount=0;
		for(int i=0,size=datas.size();i<size;i++){
			parentData=datas.get(i);
			if(parentData.getChildLevel()==level){//root
				if(parentData.getParentId()==sheetLineType.getParamsParentId()){
					//构建Cell对象
					cell=getSheetCell(parentData);
					boolean isHaveChild=true;
					for(int j=i+1;j<datas.size();j++){
						childData=datas.get(j);
						if(childData.getParentId()==parentData.getId()&&childData.getChildLevel()==level+1){
							deepCount=1;
							cell=getAllChildSheetCell(cell, datas, childData.getParentId(), j, 1);
							isHaveChild=false;
						}
					}
					if(isHaveChild){//没有子节点
						cell=setOnlyParentCell(cell, 0);
					}
					((SheetParamsLine)compent).addChild(cell);
				}
			}
		}
		((SheetParamsLine)compent).setDeepCount(deepCount);
	}

	
	private SheetCell getAllChildSheetCell(SheetCell parentCell,List<ExcelSheetDataInfo> datas, int parentId,int stemp,int topDeepCount) {
		ExcelSheetDataInfo info=datas.get(stemp);
		SheetCell cell=null;
		if(info.getParentId()==parentId){
			topDeepCount++;
			cell=getSheetCell(info);
			for(int i=stemp+1;i<datas.size();i++){
				cell=getAllChildSheetCell(cell, datas, info.getId(), i, topDeepCount);
			}
			if(topDeepCount>deepCount){
				deepCount=topDeepCount;
			}
			addChidrenSheetCell(parentCell, cell);
		}
		
		return parentCell;
	}
	
	
	private SheetCell setOnlyParentCell(SheetCell cell,int topDeepCount){
		topDeepCount++;
		if(topDeepCount>deepCount){
			deepCount=topDeepCount;
		}
		leafCount++;
		return cell;
	}
	
	
	public static void main(String[] args) {
		List<ExcelSheetDataInfo> datas =new ArrayList<ExcelSheetDataInfo>();
		
		ExcelSheetDataInfo dataInfo_1=new ExcelSheetDataInfo();
		dataInfo_1.setChildLevel(1);
		dataInfo_1.setId(-101);
		dataInfo_1.setParentId(-100);
		dataInfo_1.setValue("合计一");
		datas.add(dataInfo_1);
		
		
		ExcelSheetDataInfo dataInfo_1_1=new ExcelSheetDataInfo();
		dataInfo_1_1.setChildLevel(2);
		dataInfo_1_1.setId(-99);
		dataInfo_1_1.setParentId(-101);
		dataInfo_1_1.setValue("指标一");
		datas.add(dataInfo_1_1);
		
		ExcelSheetDataInfo dataInfo_1_2=new ExcelSheetDataInfo();
		dataInfo_1_2.setChildLevel(2);
		dataInfo_1_2.setId(-98);
		dataInfo_1_2.setParentId(-101);
		dataInfo_1_2.setValue("指标二");
		datas.add(dataInfo_1_2);
		
		ExcelSheetDataInfo dataInfo_2=new ExcelSheetDataInfo();
		dataInfo_2.setChildLevel(1);
		dataInfo_2.setId(-97);
		dataInfo_2.setParentId(-100);
		dataInfo_2.setValue("a");
		datas.add(dataInfo_2);
		
		
		ExcelSheetDataInfo dataInfo_2_1=new ExcelSheetDataInfo();
		dataInfo_2_1.setChildLevel(2);
		dataInfo_2_1.setId(-96);
		dataInfo_2_1.setParentId(-97);
		dataInfo_2_1.setValue("a_指标一");
		datas.add(dataInfo_2_1);
		
		
		ExcelSheetDataInfo dataInfo_3=new ExcelSheetDataInfo();
		dataInfo_3.setChildLevel(1);
		dataInfo_3.setId(-95);
		dataInfo_3.setParentId(-100);
		dataInfo_3.setValue("b");
		datas.add(dataInfo_3);
		
		
		ExcelSheetDataInfo dataInfo_3_1=new ExcelSheetDataInfo();
		dataInfo_3_1.setChildLevel(2);
		dataInfo_3_1.setId(-94);
		dataInfo_3_1.setParentId(-95);
		dataInfo_3_1.setValue("b_指标一");
		datas.add(dataInfo_3_1);
		
		ExcelSheetDataInfo dataInfo_4=new ExcelSheetDataInfo();
		dataInfo_4.setChildLevel(1);
		dataInfo_4.setId(-93);
		dataInfo_4.setParentId(-100);
		dataInfo_4.setValue("c");
		datas.add(dataInfo_4);
		
		
		ExcelSheetDataInfo dataInfo_4_1=new ExcelSheetDataInfo();
		dataInfo_4_1.setChildLevel(2);
		dataInfo_4_1.setId(-92);
		dataInfo_4_1.setParentId(-93);
		dataInfo_4_1.setValue("c_指标一");
		datas.add(dataInfo_4_1);
		
		
		ExcelSheetDataInfo dataInfo=new ExcelSheetDataInfo();
		dataInfo.setChildLevel(1);
		dataInfo.setId(-92);
		dataInfo.setParentId(-100);
		dataInfo.setValue("合计二");
		datas.add(dataInfo);
		
		
		ExcelSheetDataInfo dataInfo1=new ExcelSheetDataInfo();
		dataInfo1.setChildLevel(2);
		dataInfo1.setId(-91);
		dataInfo1.setParentId(-92);
		dataInfo1.setValue("合计二指标一");
		datas.add(dataInfo1);
		
		ExcelSheetDataInfo dataInfo2=new ExcelSheetDataInfo();
		dataInfo2.setChildLevel(2);
		dataInfo2.setId(-90);
		dataInfo2.setParentId(-92);
		dataInfo2.setValue("合计二指标二");
		datas.add(dataInfo2);
		
		ExcelSheetLineType sheetType=new ExcelSheetLineType();
		sheetType.setParamsParentId(-100);
		try{
			EngineDataExcelSheetLineParams params=new  EngineDataExcelSheetLineParams(1);
			params.data2SheetCompent(datas, sheetType);
			SheetParamsLine line=(SheetParamsLine)params.getSheetGenericCompent();
			System.out.println(line);
		}catch(IOfficeException e){
			System.out.println(e.toString());
		}
	}
}
